package com.iflytek.aisol.audit.iflytek.strategy.manage.handler.impl;

import cn.hutool.json.JSONUtil;
import com.iflytek.aisol.audit.iflytek.strategy.manage.config.CustomException;
import com.iflytek.aisol.audit.iflytek.strategy.manage.entity.ChannelConfig;
import com.iflytek.aisol.audit.iflytek.strategy.manage.entity.FrChannelConfig2Self;
import com.iflytek.aisol.audit.iflytek.strategy.manage.handler.ChannelHandler;
import com.iflytek.aisol.audit.iflytek.strategy.manage.mapper.ChannelConfigMapper;
import com.iflytek.aisol.audit.iflytek.strategy.manage.mapper.fr.FrChannelConfig2SelfMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
@RequiredArgsConstructor
public class AggDefaultChannelHandler implements ChannelHandler {
    private final FrChannelConfig2SelfMapper frChannelConfig2SelfMapper;
    private final ChannelConfigMapper channelConfigMapper;

    @Override
    public void handle(ChannelConfig channelConfig) {
        //2 如果传的是 agg_default 类型的channel，则写入frChannelConfig2SelfMapper表中
        //2.1 首先获取 agg_suggest 里面所有的 instance_id，然后检查是不是包含 child_instances
        //2.1 如果是，则校验通过，可以插入级联表中
        Long id = channelConfig.getId();
        Map<String, Object> jsonMap = JSONUtil.toBean(channelConfig.getConfig(), Map.class);
        // 获取 child_instances 和 agg_suggest
        // 这里的childInstances 建议换成数据库中的非主键的channel_id ,前端提交到后端服务id不建议使用主键id
        List<String> childInstances = (List<String>) jsonMap.get("child_instances");
        List<Map<String, Object>> aggSuggest = (List<Map<String, Object>>) jsonMap.get("agg_suggest");
        // 获取所有的 instance_id
        Set<String> instanceIds = new HashSet<>();
        for (Map<String, Object> suggestion : aggSuggest) {
            List<Map<String, String>> condition = (List<Map<String, String>>) suggestion.get("condition");
            for (Map<String, String> cond : condition) {
                String instanceId = cond.get("instance_id");
                instanceIds.add(instanceId);
            }
        }
        // 校验 child_instances 是否是 instance_id 的子集
        boolean isValid = childInstances.stream().allMatch(instanceIds::contains);
        if (!isValid) {
            // 校验不通过
            throw new CustomException("child_instances 不是 instance_id 的子集");
        }
        // 校验通过，可以插入级联表中 需要校验级联的服务和channel是启用状态
        for (String child : childInstances) {
            // 校验级联的c_channel是否是启用状态
            if (channelConfigMapper.findWithChannelIdAndNoDeleteAndVersionMax(child).isEmpty()) {
                throw new CustomException("该条记录"+child+"未启用，不可插入级联表");
            }
            // 获取c_channel记录的主键id
            List<ChannelConfig> c_channelConfig = channelConfigMapper.findWithChannelIdAndNoDeleteAndVersionMax(child);
            if (c_channelConfig.get(0)==null) {
                throw new CustomException("找不到版本号最大且未删除的channel记录");
            }
            Long cId = c_channelConfig.get(0).getId();
            FrChannelConfig2Self frChannelConfig2Self = new FrChannelConfig2Self();
            frChannelConfig2Self.setPChannelConfigId(id);
            frChannelConfig2Self.setCChannelConfigId(cId);
            frChannelConfig2SelfMapper.save(frChannelConfig2Self);
        }
    }
}